package cn.tx.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import cn.tx.model.Dep;
import cn.tx.model.Emp;
import cn.tx.model.OrderDetail;
import cn.tx.model.OrderModel;
import cn.tx.model.Product;
import cn.tx.model.Supplier;
import cn.tx.query.OrderModelQuery;
import cn.tx.service.ConsoleLogService;
import cn.tx.service.DepService;
import cn.tx.service.OrderModelService;
import cn.tx.service.SupplierService;
import cn.tx.util.ERPConstants;
import cn.tx.util.JSONWriter;
import cn.tx.util.Page;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class OrderTranAction extends BaseAction implements ModelDriven<OrderModel>{
	
	private OrderModel orderModel = new OrderModel();
	
	private OrderModelService orderModelService;
	
	private SupplierService supplierService;
	
	private DepService depService;
	
	private Integer depId;
	
	

	public Integer getDepId() {
		return depId;
	}

	public void setDepId(Integer depId) {
		this.depId = depId;
	}

	public DepService getDepService() {
		return depService;
	}

	public void setDepService(DepService depService) {
		this.depService = depService;
	}

	public SupplierService getSupplierService() {
		return supplierService;
	}

	public void setSupplierService(SupplierService supplierService) {
		this.supplierService = supplierService;
	}

	public OrderModel getOrderModel() {
		return orderModel;
	}

	public void setOrderModel(OrderModel orderModel) {
		this.orderModel = orderModel;
	}

	/**
	 * 查询对象
	 */
	private OrderModelQuery erpQuery = new OrderModelQuery();
	
	
	public OrderModelService getOrderModelService() {
		return orderModelService;
	}

	public void setOrderModelService(OrderModelService orderModelService) {
		this.orderModelService = orderModelService;
	}
	

	public OrderModelQuery getErpQuery() {
		return erpQuery;
	}

	public void setErpQuery(OrderModelQuery erpQuery) {
		this.erpQuery = erpQuery;
	}

	public String orderTran_tran_list(){
		//获得action的容器
		ActionContext context = ActionContext.getContext();
		String orderTypeStr = erpQuery.getOrderTypeStr();
		//把逗号分隔的字符串拆分出来
		if(StringUtils.isNotBlank(orderTypeStr)){
			String[] split = orderTypeStr.split(",");
			List<Integer> ots = new ArrayList<Integer>();
			for(String str : split){
				ots.add(new Integer(str));
			}
			//把多个订单类型的数据设置上
			erpQuery.setOrderTypes(ots);
			
		}
		exclude.add("orderTypeStr");
		Page page = orderModelService.selectObjectByConditionPage(erpQuery, exclude);
		context.put("page", page);
		
		return SUCCESS;
	}
	public String orderTran_tran_taskList(){
		//获得action的容器
		ActionContext context = ActionContext.getContext();
		
		Map<String, Object> session2 = context.getSession();
		Emp emp = (Emp) session2.get("user");
		String orderTypeStr = erpQuery.getOrderTypeStr();
		erpQuery.setCompleterId(emp.getEmpId());
		//把逗号分隔的字符串拆分出来
		if(StringUtils.isNotBlank(orderTypeStr)){
			String[] split = orderTypeStr.split(",");
			List<Integer> ots = new ArrayList<Integer>();
			for(String str : split){
				ots.add(new Integer(str));
			}
			//把多个订单类型的数据设置上
			erpQuery.setOrderTypes(ots);
			
		}
		exclude.add("orderTypeStr");
		Page page = orderModelService.selectObjectByConditionPage(erpQuery, exclude);
		context.put("page", page);
		
		return SUCCESS;
	}

	
	public String orderTran_tran_view(){
		ActionContext context = ActionContext.getContext();
		orderModel = orderModelService.getObjectById(orderModel.getOrderId());
		Dep dep = depService.getObjectById(depId);
		Set<Emp> emps = dep.getEmps();
		context.put("emps", emps);
		return SUCCESS;
	}
	public void orderTran_tran_finishBuyOrder(){
		orderModel = orderModelService.getObjectById(orderModel.getOrderId());
		orderModel.setOrderState(ERPConstants.ORDER_FINISH);
		orderModel.setOrderType(ERPConstants.ORDER_TYPE_INSTOCK);
		orderModel.setEndTime(new Date());
		orderModelService.update(orderModel);
		JSONWriter.writeString("yes", response);
	}
	/**
	 * 查看订单的详细
	 * @return
	 */
	public String orderTran_tran_orderDetail(){
		orderModel = orderModelService.getObjectById(orderModel.getOrderId());
		
		return SUCCESS;
	}
	public void orderTran_tran_assgin(){
		Emp emp = orderModel.getOrderCompleter();
		orderModel = orderModelService.getObjectById(orderModel.getOrderId());
		orderModel.setOrderCompleter(emp);
		orderModel.setOrderState(ERPConstants.ORDER_BUYING);
		orderModelService.update(orderModel);
		JSONWriter.writeString("yes", response);
	}
	
	
	@Override
	public OrderModel getModel() {
		return orderModel;
	}
	
	
	
	

}
